package com.bicon.botu.mall.base.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.bicon.botu.mall.base.common.MallHomePageConstants;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.core.base.common.utils.StringUtils;
import com.bicon.botu.mall.base.entity.CarefullyChosen;
import com.bicon.botu.mall.base.service.ICarefullyChosenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 为你精选 前端控制器
 *
 * @author 张凡
 * @date 2017/7/19 下午 14:49
 */
@RestController
@CrossOrigin
@RequestMapping("/V1.0.0/homePage/carefullyChosen")
public class CarefullyChosenController extends BaseController {
    @Autowired
    ICarefullyChosenService iCarefullyChosenService;

    /**
     * 批量新增为你精选图
     *
     * @param carefullyChosenJson 精选图对象集合
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public BaseResult insert(@RequestParam(value = "carefullyChosenJson") String carefullyChosenJson) {
        List<CarefullyChosen> carefullyChosenList = JSONObject.parseArray(carefullyChosenJson, CarefullyChosen.class);
        Boolean flag = iCarefullyChosenService.insert(carefullyChosenList);
        if (flag) {
            return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200);
        } else {
            return new BaseResult(Constants.RESPONSE_CODE_500, Constants.RESPONSE_MESSAGE_500);
        }
    }

    /**
     * 删除为你精选图
     *
     * @param idJson 为你精选Id集合
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public BaseResult delete(@RequestParam("idJson") String idJson) {
        return iCarefullyChosenService.delete(idJson);
    }

    /**
     * 修改为你精选标题
     *
     * @param title
     * @return
     */
    @RequestMapping(value = "/updateTitle", method = RequestMethod.POST)
    public BaseResult updateTitle(@RequestParam("title") String title) {
        return iCarefullyChosenService.updateTitle(title);
    }

    /**
     * 修改为你精选图
     *
     * @param carefullyChosenJson 精选图对象集合
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult update(@RequestParam(value = "carefullyChosenJson") String carefullyChosenJson) {
        if (StringUtils.isBlank(carefullyChosenJson)) {
            return sendResult400();
        }
        List<CarefullyChosen> carefullyChosenList = JSONObject.parseArray(carefullyChosenJson, CarefullyChosen.class);
        return iCarefullyChosenService.update(carefullyChosenList);
    }

    /**
     * 初始化为你精选图到redis
     *
     * @return
     */
    @RequestMapping(value = "/initCarefullyChosenToRedis", method = RequestMethod.POST)
    public BaseResult initCarefullyChosenToRedis() {
        return iCarefullyChosenService.initCarefullyChosenToRedis();
    }

    /**
     * 获取为你精选图详情
     *
     * @return
     */
    @RequestMapping(value = "/getCarefullyChosenDetail", method = RequestMethod.GET)
    public BaseResult getCarefullyChosenDetail() {
        //查询出所有正常的为你精选图
        List<CarefullyChosen> carefullyChosenList = new CarefullyChosen().selectList(new EntityWrapper<CarefullyChosen>()
                .eq("logic_delete", MallHomePageConstants.DEL_LOGIC_NORMAL).orderBy("sort", true));
        return new BaseResult(MallHomePageConstants.RESPONSE_CODE_200, MallHomePageConstants.RESPONSE_MESSAGE_200, carefullyChosenList);
    }

    /**
     * 修改排序
     *
     * @param carefullyChosenJson 精选图对象集合
     * @return
     */
    @RequestMapping(value = "/updateSort", method = RequestMethod.POST)
    public BaseResult updateSort(@RequestParam(value = "carefullyChosenJson") String carefullyChosenJson) {
        if (StringUtils.isBlank(carefullyChosenJson)) {
            return sendResult400();
        }
        List<CarefullyChosen> carefullyChosenList = JSONObject.parseArray(carefullyChosenJson, CarefullyChosen.class);
        return iCarefullyChosenService.updaterSort(carefullyChosenList);
    }

}
